package batch.import_user;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.log4j.*;

import java.util.ArrayList;
import java.sql.*;

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;

import batch.common.db.ConnectionManager;

public class chgPasswd
{
    //SELECT USERID
    //     , CASE WHEN SUBSTR(BIRTHDAY, 5, 2) > 12
    //            THEN 'PW' || TO_CHAR(TO_DATE(BIRTHDAY, 'YYYYDDMM'), 'DDMMYYYY')
    //            ELSE 'PW' || TO_CHAR(TO_DATE(BIRTHDAY, 'YYYYMMDD'), 'DDMMYYYY')
    //        END PASSWD
    //     , FNAME
    //     , LNAME
    //     , GENDER
    //     , EMAIL
    //     , BIRTHDAY
    //  FROM G_GBR_IMPORT_TEMP

    public static void main(String[] args)
    {
        Logger.getLogger("import_user.chgPasswd").info("( * ) Start.");

        chgPasswd obj = new chgPasswd();
        obj.init();

        Logger.getLogger("import_user.chgPasswd").info("( * ) End.");
    }


    public void init(){
        Connection conn = null;

        int iRE = 0;

        ArrayList params = new ArrayList();

        StringBuffer strQuery = new StringBuffer()
                               .append("SELECT USER_NO                                           \n")
                               .append("     , BIRTH                                             \n")
                               .append("  FROM G_MB_MEMBER                                       \n")
                               .append(" WHERE MEMBERGBN IN ('10437101') \n");

        Statement stmt = null;
        ResultSet rs = null;

        int i = 0;

        try{
            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(false);

            QueryRunner qRunner = new QueryRunner();

            stmt = conn.createStatement();
            rs = stmt.executeQuery(strQuery.toString());

            String sUserNo   = "";
            String sBirth    = "";
            String strPasswd = "";

            while(rs.next()){
                i++;

                sUserNo = String.valueOf(rs.getLong(1));
                sBirth = rs.getString(2);

                //Rule : Birthday - DDMMYYYY
                strPasswd = "pw" + sBirth.substring(6, 8) + sBirth.substring(4, 6) + sBirth.substring(0, 4);
                System.out.println("strPasswd1=" + strPasswd);

                strPasswd = this.getMD5(strPasswd);
                System.out.println("strPasswd2=" + strPasswd);

                strQuery.setLength(0);
                strQuery = new StringBuffer()
                          .append("UPDATE G_MB_MEMBER         \n")
                          .append("   SET PASSWORD_NEW = ?    \n")
                          .append(" WHERE USER_NO = ?         \n");

                params.clear();
                params.add(strPasswd);
                params.add(sUserNo  );

                iRE = qRunner.update(conn, strQuery.toString(), params.toArray());

                if(i % 500 == 0){
                    Logger.getLogger("import_user.chgPasswd").info("here we are == " + i);
                }
            }

            conn.commit();
        }
        catch(Exception ex) {
            if (conn != null) try { conn.rollback(); } catch (Exception se) {}
            ex.printStackTrace();
            Logger.getLogger("import_user.chgPasswd").info(ex.toString());
        }
        finally {
            if (rs != null) try { rs.close(); } catch (Exception se) {}
            if (stmt != null) try { stmt.close(); } catch (Exception se) {}
            if (conn != null) try { conn.close(); } catch (Exception se) {}
        }
    }

    public static String getMD5(String strPWD) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bDigest   = md.digest(strPWD.getBytes());

        BASE64Encoder b64e   = new BASE64Encoder();
        String strConvertPWD = b64e.encode(bDigest);

  	  	return strConvertPWD;
    }
}
